package com.baby.movie.widget;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baby.movie.R;
import com.baby.movie.model.Video;
import com.baby.movie.util.AppUtil;
import com.baby.movie.util.ImageUtil;

/**
 * @author seven
 * @version V1.0
 * @date 13-11-1
 * @description
 */
public class SlideShowAdapter extends PagerAdapter {
	
	private Context mContext;
	
	private LayoutInflater mInflater;

	private List<Video> mItems;
	
	private int mStartPosition = 0;
	
	public boolean isGame = true;
	
	public int getStartPosition() {
		return mStartPosition;
	}

	public SlideShowAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
	}
	
	public void setData(List<Video> items) {
		mItems = items;
		notifyDataSetChanged();
	}
	
	private int size(List<?> list) {
		return list == null ? 0 : list.size();
	}
	
	@Override
	public int getCount() {
		return size(mItems);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View layout = (View) mInflater.inflate(R.layout.item_slideshow,
				container, false);

		((ViewPager) container).addView(layout, 0);
		setData(layout, position);
		return layout;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == (View) arg1;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	private void setData(View layout, int page) {
		final Video video = mItems.get(page);
		ImageView imageView = (ImageView) layout.findViewById(R.id.ivImage);
		TextView tvTitle = (TextView) layout.findViewById(R.id.tvTitle);
		View vItem = layout.findViewById(R.id.vItem);
		loadImage(video.Cover, imageView);
		tvTitle.setText(video.Title);
		vItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AppUtil.showMtDetail(mContext, video);
			}
		});
	}
	
	private void loadImage(String url, ImageView imageView) {
		if (TextUtils.isEmpty(url)) {
			return;
		}
		ImageUtil.loadImage(url, imageView);
	}
}